﻿<?php
require_once 'application/controllers/admin.php';

############################################
#	Контролер за администрација на насоки  #
############################################

class adminStudies extends Admin{

	public function __construct(){
		parent::__construct();
	}

	public function index(){
		$this->studies();
	}

	//Функција за приказ на сите насоки
	public function allStudies(){
		$user = $this->userCheck('studies','admin/main');

		$this->load->model('studies_model');
		$this->load->helper('form');
		$data['title'] = 'ФИНКИ - Администратор - Студии';
		$data['include']="admin/adminContent";
		$data['content'] = 'adminStudies/allStudies';
		$data['navbar'] = 'admin/main';
		$data['studies'] = $this->studies_model->joinSort('studiescontents', 'studies.idStuPk = studiescontents.idStuFk',array('studiescontents.language'=>'mk'),'idStuPk','asc');

		$this->load->view('masterPage', $data);
	}
	
	//Функција за приказ на одредена насока
	public function studies($id){
		$user = $this->userCheck('studies','admin/main');

		$this->load->model('studies_model');
		$this->load->helper('form');

		$study = $this->studies_model->joinId('studiescontents', 'studies.idStuPk = studiescontents.idStuFk',array('studiescontents.language'=>'mk'),'idStuPk','asc');
		$data['title'] = 'ФИНКИ - Администратор - '.$study->name;
		$data['include']="admin/adminContent";
		$data['content'] = 'adminStudies/studies';
		$data['navbar'] = 'admin/main';
		$data['study'] = $study;

		$this->load->view('masterPage', $data);
	}
	
	//Функција за додавање на насока
	public function addStudy(){
		$user = $this->userCheck('studies','admin/main');

		$this->load->helper('form');
		$data['title'] = 'ФИНКИ - Администратор - Додади насока';
		$data['include']="admin/adminContent";
		$data['content'] = 'adminStudies/addStudy';
		$data['navbar'] = 'admin/main';

		$this->load->view('masterPage', $data);
	}
	
	//Функција за уредување на насока
	public function editStudy($id, $lang){
		$user = $this->userCheck('studies','admin/main');

		$this->load->model('studies_model');
		$this->load->helper('form');

		$study = $this->studies_model->joinId('studiescontents', 'studies.idStuPk = studiescontents.idStuFk',array('studiescontents.language'=>$lang),'idStuPk','asc');

		$data['title'] = $study->name?:'Промени насока';
		$data['include']="admin/adminContent";
		$data['content'] = 'adminStudies/editStudy';
		$data['navbar'] = 'admin/main';
		$data['theStudy'] = $study;

		$this->load->view('masterPage', $data);
	}
	
	//Функција за валидација и внесување на насока во база
	public function processStudies(){
		$user = $this->userCheck('admin','admin/main');

		if(!$this->input->post('submit'))
			redirect('admin/main','refresh');

		$this->load->library('form_validation');
		$this->load->model('studies_model');
		$this->load->model('studiescontents_model');

		$this->form_validation->set_rules('name', 'име', 'required|xss_clean');
		$this->form_validation->set_rules('type', 'тип', 'xss_clean');
		$this->form_validation->set_rules('transfered', 'пренесена', 'xss_clean');
		$this->form_validation->set_rules('language', 'јазик', 'xss_clean');
		$this->form_validation->set_rules('summary', 'резиме', 'required|xss_clean');
		$this->form_validation->set_rules('tip', 'тип', 'xss_clean');

		if ($this->form_validation->run() == FALSE)
		{
			redirect('error','refresh');
		}

		$name = $this->input->post('name', TRUE);
		$type = $this->input->post('type', TRUE);
		$transfered = $this->input->post('transfered', TRUE);
		$language = $this->input->post('language', TRUE);
		$summary = $this->input->post('summary', TRUE);
		$tip = $this->input->post('tip', TRUE);

		$dataStudies = array( 'transfered' => $transfered,
				'type' => $type,
		);

		$id = $this->studies_model->insert($dataStudies);
		$dataContentsMK = array( 'name'=>$name,
				'summary'=>$summary,
				'language'=>'mk',
				'idStuFk'=>$id
		);
		$dataContentsEN = array( 'name'=>"",
				'summary'=>"",
				'language'=>'en',
				'idStuFk'=>$id
		);
		$this->studiescontents_model->insert($dataContentsMK);
		$this->studiescontents_model->insert($dataContentsEN);

		redirect('adminStudies/studies/'.$id,'refresh');
	}
	
	//Функција за валидација и update на насока во база
	public function updateStudies($id){
		$user = $this->userCheck('admin','admin/main');

		if(!$this->input->post('submit'))
			redirect('admin/main','refresh');

		$this->load->library('form_validation');
		$this->load->model('studies_model');
		$this->load->model('studiescontents_model');

		$this->form_validation->set_rules('name', 'име', 'required|xss_clean');
		$this->form_validation->set_rules('type', 'тип', 'xss_clean');
		$this->form_validation->set_rules('transfered', 'пренесена', 'xss_clean');
		$this->form_validation->set_rules('language', 'јазик', 'xss_clean');
		$this->form_validation->set_rules('summary', 'резиме', 'required|xss_clean');
		$this->form_validation->set_rules('tip', 'тип', 'xss_clean');

		if ($this->form_validation->run() == FALSE)
		{
			redirect('error','refresh');
		}

		$name = $this->input->post('name', TRUE);
		$type = $this->input->post('type', TRUE);
		$transfered = $this->input->post('transfered', TRUE);
		$language = $this->input->post('language', TRUE);
		$summary = $this->input->post('summary', TRUE);
		$tip = $this->input->post('tip', TRUE);

		$dataStudies = array( 'transfered' => $transfered,
				'type' => $type,
		);

		$this->studies_model->update(array('idStuPk'=>$id),$dataStudies);

		$dataContents = array( 'name'=>$name,
				'summary'=>$summary,
				'language'=>$language,
		);
		$this->studiescontents_model->update(array('idStuFk'=>$id,'language'=>$language),$dataContents);

		redirect('adminStudies/studies/'.$id,'refresh');
	}
	
	//Функција за бришење на студии
	public function deleteStudies(){
		$user = $this->userCheck('studies','admin/main');

		$this->load->model('studies_model');
		$this->load->model('studiescontents_model');
		$this->load->model('studiescourses_model');

		$studies = $this->studies_model->getAll();

		foreach($studies as $study)
			if($this->input->post($study->idStuPk)!=null){
			$this->studies_model->delete(array('idStuPk'=>$study->idStuPk));
			$this->studiescontents_model->delete(array('idStuFk'=>$study->idStuPk));
			$this->studiescourses_model->delete(array('idStuFk'=>$study->idStuPk));
		}

		redirect('adminStudies/allStudies','refresh');
	}
	
	//Функција за приказ и додавање на предмет во насока
	public function courses($id){
		$user = $this->userCheck('studies','admin/main');

		$this->load->model('studiescourses_model');
		$this->load->model('courses_model');
		$this->load->helper('form');

		$data['title'] = 'ФИНКИ - Администратор - Студии';
		$data['include']="admin/adminContent";
		$data['content'] = 'adminStudies/courses';
		$data['navbar'] = 'admin/main';
		$data['courses'] = $this->studiescourses_model->joinSort('courses', 'studiescourses.idCrsFk = courses.idCrsPk',array('studiescourses.idStuFk'=>$id),'semester','asc');
		$data['allCourses'] = $this->courses_model->getAll();
		$data['id'] = $id;

		$this->load->view('masterPage', $data);
	}
	
	//Функција за додавање на предмет во насока
	public function addCourse($id){
		$user = $this->userCheck('studies','admin/main');

		$this->load->model('studiescourses_model');

		if(!$this->input->post('submit'))
			redirect('admin/main','refresh');

		$this->load->library('form_validation');

		$this->form_validation->set_rules('semester', 'семестар', 'xss_clean');
		$this->form_validation->set_rules('type', 'тип', 'xss_clean');
		$this->form_validation->set_rules('course', 'предмет', 'xss_clean');

		if ($this->form_validation->run() == FALSE)
		{
			redirect('error','refresh');
		}

		$idCourse = $this->input->post('course', TRUE);
		$type = $this->input->post('type', TRUE);
		$semester = $this->input->post('semester', TRUE);


		$data = array(
				'semester' => $semester,
				'type' => $type,
				'idStuFk' => $id,
				'idCrsFk' => $idCourse
		);

		$this->studiescourses_model->insert($data);

		redirect('adminStudies/courses/'.$id);

	}
	
	//Функција за бришење за предмети од насока
	public function deleteCourses($id){
		$user = $this->userCheck('studies','admin/main');

		$this->load->model('studiescourses_model');

		$courses = $this->studiescourses_model->getWhere(array('idStuFk'=>$id));

		foreach($courses as $course)
			if($this->input->post($course->idScrPk)!=null){
			$this->studiescourses_model->delete(array('idScrPk'=>$course->idScrPk));
		}

		redirect('adminStudies/courses/'.$id,'refresh');
	}
	
	//Функција за приказ на профили на одредена насока
	public function profiles($id){
		$user = $this->userCheck('studies','admin/main');
	
		$this->load->model('studiesprofiles_model');
		$this->load->model('profilescontents_model');
		$this->load->helper('form');
	
		$data['title'] = 'ФИНКИ - Администратор - Студии';
		$data['include']="admin/adminContent";
		$data['content'] = 'adminStudies/profiles';
		$data['navbar'] = 'admin/main';
		$data['profiles'] = $this->studiesprofiles_model->joinSort('profilescontents', 'studiesprofiles.idPrfFk = profilescontents.idPrfFk',array('studiesprofiles.idStuFk'=>$id, 'profilescontents.language'=>'mk'),'idSprPk','asc');
		$data['allProfiles'] = $this->profilescontents_model->getWhere(array('language'=>'mk'));
		$data['id'] = $id;
	
		$this->load->view('masterPage', $data);
	}
	
	//Функција за додавање на профил на насока
	public function addProfile($id){
		$user = $this->userCheck('studies','admin/main');
	
		$this->load->model('studiesprofiles_model');
	
		if(!$this->input->post('submit'))
			redirect('admin/main','refresh');
	
		$this->load->library('form_validation');
	
		$this->form_validation->set_rules('profile', 'насока', 'xss_clean');
	
		if ($this->form_validation->run() == FALSE)
		{
			redirect('error','refresh');
		}
	
		$idProfile = $this->input->post('profile', TRUE);
	
	
		$data = array(
				'idPrfFk' => $idProfile,
				'idStuFk' => $id
		);
	
		$this->studiesprofiles_model->insert($data);
	
		redirect('adminStudies/profiles/'.$id);
	
	}
	
	//Функција за бришење на профили од насока
	public function deleteProfiles($id){
		$user = $this->userCheck('studies','admin/main');
	
		$this->load->model('studiesprofiles_model');
	
		$profiles = $this->studiesprofiles_model->getWhere(array('idStuFk'=>$id));
	
		foreach($profiles as $profile)
			if($this->input->post($profile->idSprPk)!=null){
			$this->studiesprofiles_model->delete(array('idSprPk'=>$profile->idSprPk));
		}
	
		redirect('adminStudies/profiles/'.$id,'refresh');
	}
	
}
